package bd

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.util.Log
import androidx.lifecycle.*
import java.util.ArrayList

class FI : ViewModel() {
    val imageSourceLiveData:MutableLiveData<ArrayList<FB>> = MutableLiveData<ArrayList<FB>>()
    val videoSourceLiveData:MutableLiveData<ArrayList<FB>> = MutableLiveData<ArrayList<FB>>()
    val allAppInfoLiveData:MutableLiveData<ArrayList<PackageInfo?>> = MutableLiveData<ArrayList<PackageInfo?>>()
    val riskAppInfoLiveData:MutableLiveData<ArrayList<PackageInfo?>> = MutableLiveData<ArrayList<PackageInfo?>>()

    fun getSDSources(){
        FF.getSourceFile(object :
            FF.OnLocationSourceListener{
            override fun locationSources(
                imgList: ArrayList<FB>,
                videoList: ArrayList<FB>
            ) {
                imageSourceLiveData.postValue(imgList)
                videoSourceLiveData.postValue(videoList)
            }
        })
    }


    private var hideIconAppList:ArrayList<String> = ArrayList()
    private var allAppInfo:ArrayList<PackageInfo?> = ArrayList()
    private var riskAppInfo:ArrayList<PackageInfo?> = ArrayList()
    /**
     * 获取手机已安装应用列表
     * @param ctx
     * @param isFilterSystem 是否过滤系统应用
     * @return
     */
    fun getAllAppInfo(ctx: Context, isFilterSystem: Boolean) {
        allAppInfo.clear()
        riskAppInfo.clear()
        val satrtTime = System.currentTimeMillis()
        var bean: FA? = null
        val packageManager = ctx.packageManager
        val list: List<PackageInfo> = packageManager.getInstalledPackages(0)

        for (p in list) {
            if(p.applicationInfo.packageName == ctx.packageName){
                continue
            }

            bean = FA()
//            bean.setIcon(p.applicationInfo.loadIcon(packageManager))
//            bean.setLabel(packageManager.getApplicationLabel(p.applicationInfo).toString())
//            bean.setPackage_name(p.applicationInfo.packageName)
            if (hideIconAppList.contains(p.applicationInfo.packageName)) {
                bean.setRisk(true)
            }
            val flags: Int = p.applicationInfo.flags
            // 判断是否是属于系统的apk
            if (flags and ApplicationInfo.FLAG_SYSTEM != 0 && isFilterSystem) {
//                bean.setSystem(true);
            } else {
                if (bean.isRisk){
                    riskAppInfo.add(p)
                }else{
                    allAppInfo.add(p)
                }
            }

        }
        riskAppInfoLiveData.postValue(riskAppInfo)
        allAppInfoLiveData.postValue(allAppInfo)

        Log.i("ddd==","检索本地应用耗时："+(System.currentTimeMillis()-satrtTime));
    }

    /**
     * 获取隐藏app icon的应用
     */
    @SuppressLint("WrongConstant")
    fun getHideIconAppList(context: Context){
        hideIconAppList.clear()
        val mPackageManager = context.packageManager
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        val list: List<ResolveInfo> = mPackageManager.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS)

        if (list.isNotEmpty()) {
            for (resolveInfo in list) {
                if(!resolveInfo.activityInfo.enabled){
                    val pkgName = resolveInfo.activityInfo.applicationInfo.packageName
                    hideIconAppList.add(pkgName)
                }
            }
        }
    }

}